define([], function () {
    require.config({
    paths: {
        'summernote': '../addons/summernote/lang/summernote-zh-CN.min'
    },
    shim: {
        'summernote': ['../addons/summernote/js/summernote', 'css!../addons/summernote/css/summernote.css'],
    }
});
require(['form', 'upload','layer'], function (Form, Upload,Layer) {
    var _bindevent = Form.events.bindevent;
    Form.events.bindevent = function (form) {
        _bindevent.apply(this, [form]);
         // 插入小程序
        var miniprogram = function(context){
            var ui = $.summernote.ui;
            var button = ui.button({
                contents: '<i class="fa fa-meanpath"/>',
                tooltip: __('插入小程序'),
                click: function () {
                    parent.Fast.api.open("general/attachment/miniprogram", __('插入小程序'), {
                        callback: function (data) {
                            var node = '';
                            if(data.type == 'text'){
                                node = $("<a data-miniprogram-appid='"+data.appid+"' href='' data-miniprogram-path='"+data.path+"'>" +data.content + "</a>");
                            }else if(data.type == 'image'){
                                node = $("<a data-miniprogram-appid='"+data.appid+"' href='' data-miniprogram-path='"+data.path+"'><img src='"+data.image+"'></a>");
                            }
                            context.invoke('insertNode', node[0]);
                            // var urlArr = data.url.split(/\,/);
                            // $.each(urlArr, function () {
                            //     var url = Fast.api.cdnurl(this);
                            //     var node = $("<a href='" + url + "'>" + url + "</a>");
                            //     context.invoke('insertNode', node[0]);
                            // });
                        }
                    });
                    return false;
                }
            });
            return button.render();
        }
        try {
            //绑定summernote事件
            if ($(".summernote,.editor", form).size() > 0) {
                require(['summernote'], function () {
                    var imageButton = function (context) {
                        var ui = $.summernote.ui;
                        var button = ui.button({
                            contents: '<i class="fa fa-file-image-o"/>',
                            tooltip: __('Choose'),
                            click: function () {
                                parent.Fast.api.open("general/attachment/select?element_id=&multiple=true&mimetype=image/*", __('Choose'), {
                                    callback: function (data) {
                                        var urlArr = data.url.split(/\,/);
                                        $.each(urlArr, function () {
                                            var url = Fast.api.cdnurl(this);
                                            context.invoke('editor.insertImage', url);
                                        });
                                    }
                                });
                                return false;
                            }
                        });
                        return button.render();
                    };
                    var attachmentButton = function (context) {
                        var ui = $.summernote.ui;
                        var button = ui.button({
                            contents: '<i class="fa fa-file"/>',
                            tooltip: __('Choose'),
                            click: function () {
                                parent.Fast.api.open("general/attachment/select?element_id=&multiple=true&mimetype=*", __('Choose'), {
                                    callback: function (data) {
                                        var urlArr = data.url.split(/\,/);
                                        $.each(urlArr, function () {
                                            var url = Fast.api.cdnurl(this);
                                            var node = $("<a href='" + url + "'>" + url + "</a>");
                                            context.invoke('insertNode', node[0]);
                                        });
                                    }
                                });
                                return false;
                            }
                        });
                        return button.render();
                    };
                   
                    $(".summernote,.editor", form).summernote({
                        height: 250,
                        lang: 'zh-CN',
                        fontNames: [
                            'Arial', 'Arial Black', 'Serif', 'Sans', 'Courier',
                            'Courier New', 'Comic Sans MS', 'Helvetica', 'Impact', 'Lucida Grande',
                            "Open Sans", "Hiragino Sans GB", "Microsoft YaHei",
                            '微软雅黑', '宋体', '黑体', '仿宋', '楷体', '幼圆',
                        ],
                        fontNamesIgnoreCheck: [
                            "Open Sans", "Microsoft YaHei",
                            '微软雅黑', '宋体', '黑体', '仿宋', '楷体', '幼圆'
                        ],
                        toolbar: [
                            ['style', ['style', 'undo', 'redo']],
                            ['font', ['bold', 'underline', 'strikethrough', 'clear']],
                            ['fontname', ['color', 'fontname', 'fontsize']],
                            ['para', ['ul', 'ol', 'paragraph', 'height']],
                            ['table', ['table', 'hr']],
                            ['insert', ['link', 'picture', 'miniprogram']],//'video',
                            ['select', ['image']],//'attachment'
                            ['view', ['fullscreen',  'help']],//'codeview',
                        ],
                        buttons: {
                            image: imageButton,
                            attachment: attachmentButton,
                            miniprogram:miniprogram
                        },
                        dialogsInBody: true,
                        followingToolbar: false,
                        callbacks: {
                            onChange: function (contents) {
                                $(this).val(contents);
                                $(this).trigger('change');
                            },
                            onInit: function () {
                            },
                            onImageUpload: function (files) {
                                var that = this;
                                //依次上传图片
                                for (var i = 0; i < files.length; i++) {
                                    Upload.api.send(files[i], function (data) {
                                        var url = Fast.api.cdnurl(data.url);
                                        $(that).summernote("insertImage", url, 'filename');
                                    });
                                }
                            },

                        }
                    });
                });
            }
        } catch (e) {

        }
        try {
            if ($(".simple-editor", form).size() > 0) {
                require(['summernote'], function () {
                    var link_create = function(context){
                        var ui = $.summernote.ui;
                        var button = ui.button({
                            contents: '<i class="fa fa-commenting"/>',
                            tooltip: __('咨询'),
                            click: function () {
                                var body = [
                                    '<div style="padding:0 15px;"><div class="form-group note-form-group">',
                                    "<label class=\"note-form-label\"></label>",
                                    '<input class="note-wechat-text form-control note-form-control note-input" type="text" />',
                                    '</div></div>',
                                    ].join('');
                                var layerIndex = Layer.open({
                                      type: 1,
                                      shade: false,
                                      title: "微信咨询", //不显示标题
                                      area: ['460px', '240px'], //宽高
                                      content: body, //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
                                      btn: ['插入']
                                      ,yes: function(index, layero){
                                        var msg = $(".note-wechat-text").val();
                                        if(msg == null || msg == ''){
                                            Layer.close(layerIndex)
                                        }
                                        var node = $("<a href='weixin://bizmsgmenu?msgmenucontent="+msg+"&msgmenuid=1"+msg+"'>"+msg+"</a>");
                                        context.invoke('insertNode',node[0]);
                                        
                                        Layer.close(layerIndex)
                                      }
                                    });
                                return false;
                            }
                        });
                        return button.render();
                    };
                    
                    $(".simple-editor", form).summernote({
                            height: 150,
                            lang: 'zh-CN',
                            fontNames: [
                                'Arial', 'Arial Black', 'Serif', 'Sans', 'Courier',
                                'Courier New', 'Comic Sans MS', 'Helvetica', 'Impact', 'Lucida Grande',
                                "Open Sans", "Hiragino Sans GB", "Microsoft YaHei",
                                '微软雅黑', '宋体', '黑体', '仿宋', '楷体', '幼圆',
                            ],
                            fontNamesIgnoreCheck: [
                                "Open Sans", "Microsoft YaHei",
                                '微软雅黑', '宋体', '黑体', '仿宋', '楷体', '幼圆'
                            ],
                            toolbar: [
                                //['style', ['style', 'undo', 'redo']],
                                ['select', ['mlink','link','miniprogram']],
                                //['insert', ['unlink']],
                                
                            ],
                            dialogsInBody: true,
                            followingToolbar: false,
                            buttons: {
                                mlink:link_create,
                                miniprogram:miniprogram
                            },
                            callbacks: {
                                onChange: function (contents) {
                                    $(this).val(contents);
                                    $(this).trigger('change');
                                },
                                onInit: function () {
                                },
                                
                            }
                    })
                })
            }
        }catch (e) {

        }
    };
});

});